<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2007/01/23 19:36:10 $)
// $RCSfile: index.php,v $ : $Revision: 1.9 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


define("SWIFT_AREA", 200);

if (defined("SWIFTCUSTOMPATH"))
{
	chdir(SWIFTCUSTOMPATH);
} else {
	chdir('./../');
}
require_once ("./swift.php");

if ($_REQUEST["_ca"] == "login") {
	$_REQUEST["password"] = preg_replace(array("/[^a-zA-Z0-9]/"), array(""), $_REQUEST["password"]);
	$staffid = $staffauth->authenticate(urldecode($_REQUEST["username"]), strtolower($_REQUEST["password"]), false, false);
	if (!empty($staffid))
	{
		require_once ("./includes/functions_xml.php");
		$xml = new XML;

		$session->insertSession(SESSION_MOBILE, $staffid);
		$staffauth->loadStaff($staffid);

		$xml->addParentTag("mobile");

			$xml->addTag("sessionid", $_SWIFT["session"]["sessionid"]);
			$xml->addTag("version", SWIFTVERSION);

			// Hardcoded module event callback, will just return false if module isnt there or isnt registered
			$events->triggerCallback("tickets", "mobileLogin");

			$xml->addTag("staffid", $staffid);

		$xml->endTag("mobile");

		$xml->echoXMLUTFWinapp();

		require_once ("./includes/functions_cron.php");
		$cron = new Cron;

		$cron->runPendingTasks();

	} else {
		// Authentication Failed
//		echo "authentication failed, no staff id returned from authentication using, Username: '". $_REQUEST["username"] ."', Password '". $_REQUEST["password"] ."'.. Staff ID returned is '". $staffid ."'... QUERY_STRING is '". $_SERVER["REQUEST_URI"] ."'";
//		print_r($_POST);
		echoWinappData("-2");
	}
} else if ($_REQUEST["_ca"] == "logout") {
	if (!$session->start(SESSION_MOBILE)) {
		// Session expired
		echoWinappData("-1");
		exit;
	}

	$events->triggerCallback("tickets", "mobileLogout");

	$session->logoutSession(SESSION_MOBILE);
	echoWinappData("1");
} else {
	if (!$session->start(SESSION_MOBILE)) {
		// Session expired
		echoWinappData("-1");
		exit;
	}

	$events->triggerEvent(EVENT_MOBILE);
}
?>